package net.xu.framework.security.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Douyin {
	
	public static void main(String[] args) throws IOException {
		System.out.println("----抖音去水印解析----");
		System.out.println("\n请输入从抖音复制的视频链接：");
		Scanner sc = new Scanner(System.in);
//		String info = sc.nextLine();
		String info = " 9.74 复制打开抖音，看看【白海波的作品】# 鉴定 # 鉴宝 # 古玉 # 古玩 # 鉴宝节... https://v.douyin.com/iNHBdgay/ 05/12 AGv:/ U@L.jC  ";
		getInfo(info);
	}
	
	
	


	//连接视频，重定向到无水印视频
	public static void jiexi(String url,String path,String name) throws IOException {
		Connection conn = Jsoup.connect(url).ignoreContentType(true);
		conn.header("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 10; COL-AL10 Build/HUAWEICOL-AL10)");
		String src = conn.execute().url().toString();
		System.out.println("\n解析完成,开始下载！\n");
		down(src ,path,name);
	}
	//下载视频
	public static void down(String src,String path,String name) {
		int byteRead;
		URL url = null;
		try {
			url = new URL(src);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}

		try {
			// 2.获取链接
			URLConnection conn = url.openConnection();
			long length = conn.getContentLengthLong();
			// 3.输入流
			InputStream inStream = conn.getInputStream();
			// 3.写入文件
			File file = new File(path+"\\去水印视频\\");
			if(!file.exists()) {
				file.mkdirs();
			}
			String saveFile = path+"\\去水印视频\\"+name+".mp4";
			FileOutputStream fs = new FileOutputStream(saveFile);
			byte[] buffer = new byte[1024];
			int i=0,j=0;
			while ((byteRead = inStream.read(buffer)) != -1) {
				i++;
				fs.write(buffer, 0, byteRead);
				if(i%500 ==0) {
					j++;
					File file2 = new File(saveFile);
					//控制输出小数点后的位数
					DecimalFormat df = new DecimalFormat("#.##");
					float f = (file2.length()/(float)length)*100;
					System.out.print("已下载："+ df.format(f) +"%\t\t");
					if(j% 5 ==0) {
						System.out.println();
					}
				}
			}
			System.out.println("\n已下载：100.00%");
			inStream.close();
			fs.close();
		} catch (FileNotFoundException e) {
			e.toString();
		} catch (IOException e) {
			e.toString();
		} catch (Exception e) {
			e.toString();
		}
	}
	
	
}

